package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    /**
     * 生成订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单信息："+ orders.toString());
        orderService.submit(orders);
        return R.success(null);
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Map<String, List<OrdersDto>>> page(Integer page, Integer pageSize){

        Page pageInfo=new Page(page,pageSize);
        LambdaQueryWrapper<Orders> qw=new LambdaQueryWrapper<>();
        qw.eq(Orders::getUserId,BaseContext.getCurrentId());
        List<Orders> ordersList = orderService.list(qw);

        orderService.page(pageInfo,qw);

        List<OrdersDto> ordersDtos=new ArrayList<>();
        ordersDtos=ordersList.stream().map((orders)->{
            OrdersDto ordersDto = new OrdersDto();


            LambdaQueryWrapper<OrderDetail> qw2=new LambdaQueryWrapper();
            qw2.eq(OrderDetail::getOrderId,orders.getId());
            List<OrderDetail> list = orderDetailService.list(qw2);
            ordersDto.setOrderDetails(list);

            BeanUtils.copyProperties(orders,ordersDto);


            return ordersDto;
        }).collect(Collectors.toList());

        Map<String, List<OrdersDto>> map = new HashMap<>();
        map.put("records",ordersDtos);


        return R.success(map);

    }

    /**
     * 订单查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, Integer number, LocalDateTime beginTime,LocalDateTime endTime){
        log.info("page={},pageSize={},number={}",page,pageSize,number);
        Long userId = BaseContext.getCurrentId();
        Page pageInfo=new Page(page,pageSize);
        LambdaQueryWrapper<Orders> qw=new LambdaQueryWrapper<>();


        qw.eq(number!=null,Orders::getNumber,number);
        qw.between(beginTime!=null,Orders::getOrderTime,beginTime,endTime);
        qw.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,qw);
        return R.success(pageInfo);
    }

    /**
     * 修改状态
     * @param orders
     * @return
     */
    @PutMapping
    public R changeStatus(@RequestBody Orders orders){
        LambdaUpdateWrapper<Orders> luw=new LambdaUpdateWrapper<>();
        luw.eq(Orders::getId,orders.getId());
        luw.set(Orders::getStatus,orders.getStatus());
        orderService.update(luw);
        return R.success(null);
    }

    /**
     * 再来一单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R again(@RequestBody Orders orders){
        //清空购物车
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(qw);

        //获取菜品
        LambdaQueryWrapper<OrderDetail> qw2=new LambdaQueryWrapper<>();
        qw2.eq(OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> list = orderDetailService.list(qw2);


        ShoppingCart shoppingCart =new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());

        for (OrderDetail orderDetail : list) {

            BeanUtils.copyProperties(orderDetail,shoppingCart);
        }
        shoppingCartService.save(shoppingCart);

        return R.success("ok");
    }

}
